﻿using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System;
namespace UnityEngine.UI
{
    [DisallowMultipleComponent]
    public class CustomScroll : LoopScrollRect
    {
        public GameObject parefab;
        //obj pool
        private Pool poolDict = null;

        public void InitPool(int size, PoolInflationType type = PoolInflationType.DOUBLE)
        {
            if (poolDict != null)
            {
                return;
            }
            else
            {
                GameObject pb = parefab;// GameObject.Instantiate(parefab);
                if (pb == null)
                {
                    Debug.LogError("[CustomScroll] Invalide prefab name for pooling : null");
                    return;
                }
                poolDict = new Pool(pb.name, pb, gameObject, size, type);
            }
        }

        /// <summary>
        /// Returns an available object from the pool 
        /// OR null in case the pool does not have any object available & can grow size is false.
        /// </summary>
        /// <param name="poolName"></param>
        /// <returns></returns>
        public override GameObject GetObjectFromPool(bool autoActive = true, int autoCreate = 0)
        {
            GameObject result = null;

            if (poolDict == null && autoCreate > 0)
            {
                InitPool(autoCreate, PoolInflationType.DOUBLE);
            }

            if (poolDict != null)
            {
                Pool pool = poolDict;
                result = pool.NextAvailableObject(autoActive);
                //scenario when no available object is found in pool
#if UNITY_EDITOR
                if (result == null)
                {
                    Debug.LogWarning("[CustomScroll]:No object available in ");
                }
#endif
            }
#if UNITY_EDITOR
            else
            {
                Debug.LogError("[CustomScroll]:Invalid pool name specified: ");
            }
#endif
            return result;
        }

        /// <summary>
        /// Return obj to the pool
        /// </summary>
        /// <param name="go"></param>
        public override void ReturnObjectToPool(GameObject go)
        {
            PoolObject po = go.GetComponent<PoolObject>();
            if (po == null)
            {
#if UNITY_EDITOR
                Debug.LogWarning("Specified object is not a pooled instance: " + go.name);
#endif
            }
            else
            {
                Pool pool = poolDict;
                if (poolDict != null)
                {
                    pool.ReturnObjectToPool(po);
                }
#if UNITY_EDITOR
                else
                {
                    Debug.LogWarning("No pool available with name: " + po.poolName);
                }
#endif
            }
        }

        /// <summary>
        /// Return obj to the pool
        /// </summary>
        /// <param name="t"></param>
        public void ReturnTransformToPool(Transform t)
        {
            if (t == null)
            {
#if UNITY_EDITOR
                Debug.LogError("[CustomScroll] try to return a null transform to pool!");
#endif
                return;
            }
            //set gameobject active flase to avoid a onEnable call when set parent
          //  t.gameObject.SetActive(false);
            t.SetParent(null, false);
            ReturnObjectToPool(t.gameObject);
        }

        protected override float GetSize(RectTransform item)
        {
            throw new NotImplementedException();
        }

        protected override float GetDimension(Vector2 vector)
        {
            throw new NotImplementedException();
        }

        protected override Vector2 GetVector(float value)
        {
            throw new NotImplementedException();
        }
    }
}
